//
//  RHomeHotPageViewController.m
//  iPlan
//
//  Created by 侯伟 on 2021/8/24.
//

#import "RHomeHotPageViewController.h"
#import "RHomeSearchBar.h"
#import "SDCycleScrollView.h"
#import "RVideoClassifyView.h"

#import "RVideoObj.h"
#import "RBannerRequest.h"
#import "RNoticeRequest.h"
#import "RHotClassifyRequest.h"

#import "RVideoDetailsViewController.h"
#import "RSBaseWebViewController.h"
#import "Founction.h"
//#import <AdSupport/AdSupport.h>

@interface RHomeHotPageViewController ()<SDCycleScrollViewDelegate>

/// 视频模块
@property(nonatomic,strong)MyLinearLayout *contentLayout;

/// 搜索
@property(nonatomic,strong)RHomeSearchBar *searchBar;

/// banner
@property(nonatomic,strong)MyLinearLayout *bannerLayout;

@property(nonatomic,strong)MyLinearLayout* noticeLayout;
/// banner
@property(nonatomic,strong)SDCycleScrollView *bannerView1;
/// banner
@property(nonatomic,strong)SDCycleScrollView *bannerView2;

/// 视频模块
@property(nonatomic,strong)MyLinearLayout *classfiyLayout;

/// 轮播图数据
@property(nonatomic,strong)NSArray *banners;

/// 轮播公告
@property(nonatomic,strong)NSArray *notices;

/// 轮播公告
@property(nonatomic,strong)NSArray *hotDatas;

@end

@implementation RHomeHotPageViewController

- (instancetype)init{
    if(self = [super init]){
        _VideoType = VideoType_Hot;
    }
    return self;
}

- (void)loadView{
    [super loadView];
    
    // 线性布局
    MyLinearLayout *linearLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    linearLayout.backgroundColor = UIColor.whiteColor;
    linearLayout.myMargin = 0;
    [self.view addSubview:linearLayout];
    
    //搜索
    RHomeSearchBar* searchBar = [RHomeSearchBar new];
    searchBar.VideoType = self.VideoType;
    searchBar.myHeight = PX(50);
    searchBar.myMargin = 0;
    [linearLayout addSubview:searchBar];
    _searchBar = searchBar;
    
    //底部滚动控件
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
    scrollView.myMargin = 0;
    scrollView.weight = 1.0;
    [linearLayout addSubview:scrollView];
    
    // 线性布局
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    contentLayout.padding = UIEdgeInsetsZero;
    contentLayout.myHorzMargin = 0;
    contentLayout.myTop = 0;
    contentLayout.heightSize.lBound(scrollView.heightSize, 10, 1);
    [scrollView addSubview:contentLayout];
    _contentLayout = contentLayout;
    
    
    MyLinearLayout* bannerLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    bannerLayout.myHeight = MyLayoutSize.wrap;
    bannerLayout.padding = UIEdgeInsetsMake(0, 16, 0, 16);
    bannerLayout.myMargin = 0;
    bannerLayout.bottomBorderline = [[MyBorderline alloc] initWithColor:HEXCOLOR(0xBEBEBE) thick:1.0f headIndent:16 tailIndent:16];
    [self.contentLayout addSubview:bannerLayout];
    _bannerLayout = bannerLayout;
    
    MyLinearLayout* classfiyLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    classfiyLayout.myHeight = MyLayoutSize.wrap;
    classfiyLayout.padding = UIEdgeInsetsMake(16, 0, 16, 0);
    classfiyLayout.myMargin = 0;
    classfiyLayout.subviewVSpace = 8;
    [self.contentLayout addSubview:classfiyLayout];
    _classfiyLayout = classfiyLayout;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupBannerView];
    
    [self requestBanner];
    
    [self requestNotice];
    
    [self requestClassification];
}

- (void)setupBannerView{
    
    CGFloat w = SCREEN_WIDTH - 2*16;
    
    SDCycleScrollView *bannerView1 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, w, w/1.7) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
    bannerView1.backgroundColor = UIColor.whiteColor;
    bannerView1.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    bannerView1.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    bannerView1.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    bannerView1.pageControlBottomOffset = PX(16);
    bannerView1.pageControlRightOffset = PX(20);
    bannerView1.autoScrollTimeInterval = 3.0;
    bannerView1.titleLabelHeight = PX(20);
    bannerView1.titleLabelTextFont = FONT_FIT(12);
    bannerView1.layer.cornerRadius = PX(5);
    bannerView1.layer.masksToBounds = YES;
    bannerView1.myHeight = w/1.7;
    bannerView1.myMargin = 0;
    bannerView1.visibility = MyVisibility_Gone;
    [self.bannerLayout addSubview:bannerView1];
    _bannerView1 = bannerView1;
    
    MyLinearLayout* noticeLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Horz)];
    noticeLayout.myHeight = PX(30);
    noticeLayout.myMargin = 0;
    noticeLayout.subviewHSpace = 8.0;
    [self.bannerLayout addSubview:noticeLayout];
    _noticeLayout = noticeLayout;
    noticeLayout.visibility = MyVisibility_Gone;
    
    UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home_notice"]];
    imageView.myLeft = 0;
    imageView.myCenterY = 0;
    [noticeLayout addSubview:imageView];
    
    SDCycleScrollView *bannerView2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, SCREEN_WIDTH - 16*2 - 30, self.view.rs_width, PX(30)) delegate:self placeholderImage:nil];
    bannerView2.titleLabelBackgroundColor = UIColor.whiteColor;
    bannerView2.titleLabelTextColor = COLOR_GRAY_BASE;
    bannerView2.scrollDirection = UICollectionViewScrollDirectionVertical;
    bannerView2.onlyDisplayText = YES;
    bannerView2.autoScrollTimeInterval = 3.0;
    bannerView2.backgroundColor = UIColor.whiteColor;
    bannerView2.myHeight = PX(30);
    bannerView2.myMargin = 0;
    bannerView2.weight = 1;
    [noticeLayout addSubview: bannerView2];
    _bannerView2 = bannerView2;
}

- (void)setupBanner{
    NSMutableArray* imageUrls = [NSMutableArray array];
    NSMutableArray* titles = [NSMutableArray array];
    for (RVideoObj* movie in self.banners) {
        [titles addObject:movie.name];
        [imageUrls addObject:movie.img];
    }
    if(imageUrls.count > 0){
        self.bannerView1.visibility = MyVisibility_Visible;
        self.bannerView1.titlesGroup = titles;
        self.bannerView1.imageURLStringsGroup = imageUrls;
    }else{
        self.bannerView1.visibility = MyVisibility_Gone;
    }
}

- (void)setupNotice{

    NSMutableArray* titles = [NSMutableArray array];
    for (Notice* notice in self.notices) {
        [titles addObject:notice.title];
    }
    if(titles.count > 0){
        self.noticeLayout.visibility = MyVisibility_Visible;
        self.bannerView2.titlesGroup = titles;
    }else{
        self.noticeLayout.visibility = MyVisibility_Gone;
    }
}

- (void)setupClassificationViews{
    
    for (int i = 0; i < self.hotDatas.count; i++) {
        RVideoClassify* hotdate = self.hotDatas[i];
        RVideoClassifyView* classification = [RVideoClassifyView new];
        classification.hotData = hotdate;
        classification.myMargin = 0;
        [self.classfiyLayout addSubview:classification];
    }
}

#pragma mark -- request

- (void)requestBanner{
    
    RBannerRequest* request = [RBannerRequest new];
    request.action = @"r_i";
    [RequestOperation encryptedRequest:request quite:YES callBack:^(HResponse * _Nullable response) {
        [[response failureDefalut] success:^(NSArray*  _Nonnull data) {
            self.banners = data;
            [self setupBanner];
        }];
    }];
}

- (void)requestNotice{
    RNoticeRequest* request = [RNoticeRequest new];
    request.action = @"r_a";
    [RequestOperation encryptedRequest:request quite:YES callBack:^(HResponse * _Nullable response) {
        [[response failureDefalut] success:^(NSArray*  _Nonnull data) {
            self.notices = data;
            [self setupNotice];
        }];
    }];
}

- (void)requestClassification{
    RHotClassifyRequest* request = [RHotClassifyRequest new];
    request.action = @"h_i_ls";
    [RequestOperation encryptedRequest:request quite:YES callBack:^(HResponse * _Nullable response) {
        [[response failureDefalut] success:^(NSArray*  _Nonnull data) {
            self.hotDatas = data;
            [self setupClassificationViews];
        }];
    }];
}

#pragma mark  -- SDCycleScrollViewDelegate
/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    if(cycleScrollView == self.bannerView1){
        RVideoObj* video = self.banners[index];
        RVideoDetailsViewController* detailController = [RVideoDetailsViewController new];
        detailController.video = video;
        [UIViewController showViewController:detailController animated:YES];
        
    }else if (cycleScrollView == self.bannerView2){
        Notice* notice = self.notices[index];
        if(!notice.url) return;
        
        [Founction openURLString:notice.url];
    }
}
@end
